// note! in si bookmarks will be delete when the file closeed!!!

//	 Name="XXX";File="X:\\XXX\\XXX.XXX";ln="XX";ich="XX"

macro BackupBookMark()
{
	cntBm = BookmarksCount()

	if(0==cntBm){
		Msg("No BookMarks")
		return
	}

	fNewWnd=false;
	hbuf=GetBufHandle("BookMark.bmk")
	if(hNil==hbuf){
		fNewWnd=true;
		hbuf=OpenBuf ("BookMark.bmk")
		if(hNil==hbuf){
			hbuf=NewBuf("BookMark.bmk")
		}
	}

	AppendBufLine(hbuf, "")
	AppendBufLine(hbuf, "//")
	AppendBufLine(hbuf, "//---------------------------")

	i=0
	while (i < cntBm){
    		bmItem = BookmarksItem(i)
//DbgMsg("bmItem", bmItem)
		AppendBufLine(hbuf, bmItem)
    		i = i + 1
    	}

	if(true==fNewWnd){
		hwnd=NewWnd(hbuf)
	}else{
		hwnd=GetWndHandle(hbuf)
	}

	MaximizeWnd (hwnd)
	SetCurrentWnd (hwnd)
}

macro DeleteAllBookMark()
{
	cntBm = BookmarksCount()
	if(0==cntBm){
		Msg("No BookMarks")
	}

	hbuf=NewBuf("")
	i=0
	while (i < cntBm){
    		bmItem = BookmarksItem(i)
//DbgMsg("bmItem", bmItem)
		AppendBufLine(hbuf, bmItem)
    		i = i + 1
    	}
    	i=0
	while (i < cntBm){
		bmkItem=GetBufLine(hbuf, i)
//DbgMsg("bmItem", bmItem)
		BookmarksDelete (bmkItem.Name)
    		i = i + 1
    	}

    	CloseBuf(hbuf)
}


macro ParseBookMark()
{
	hbuf=GetCurrentBuf()
	hwnd=GetCurrentWnd()
	if( (hNil==hbuf) || (hNil==hwnd) ){
		return
	}
	
//Name="XXX";File="X:\\XXX\\XXX.XXX";ln="XX";ich="XX"
	pattern="^\\w*Name=\".+\";File=\".+\";ln=\".+\";ich=\".+\"$"
//DbgMsg("pattern", pattern)

	lnStart=0
	while(1){
		selrec=SearchInBuf(hbuf, pattern, lnStart, 0, 0, 1, 0)
		if(""==selrec){
			break
		}
		lnStart=selrec.lnLast+1

		bmItem=GetBufLine(hbuf, selrec.lnFirst)
		SetSourceLink (hbuf, selrec.lnFirst, bmItem.File, bmItem.ln)
	}
	
	SetCurrentWnd(hwnd)
}


macro RestoreBookMark()
{
	hbuf=GetCurrentBuf()
	hwnd=GetCurrentWnd()
	if( (hNil==hbuf) || (hNil==hwnd) ){
		return
	}
	
//Name="XXX";File="X:\\XXX\\XXX.XXX";ln="XX";ich="XX"
	pattern="^\\w*\\(Name=\".+\";File=\".+\";ln=\".+\";ich=\".+\"\\)$"
//DbgMsg("pattern", pattern)

	bufSch=NewBuf("")
	bmkCnt=0
	lnStart=0
	while(1){
		ClearBuf(bufSch)
		selRst=SearchInBufEx (hBuf, pattern, lnStart, 0, 1, 1, 0, bufSch, 0)
		if(""==selRst){
			break
		}
		lnStart=selRst.lnLast+1

		bmItem=GetBufLine(bufSch, 1)
    		if(True==BookmarksAdd (bmItem.Name, bmItem.File, bmItem.ln, bmItem.ich)){
    			bmkCnt=bmkCnt+1
	    	}
	    	else {
	    		// to add a bookmark, the file must be opened first
	    		hbufBmkFile=OpenBuf(bmItem.File)
	    		if(False!=hbufBmkFile){
	    			NewWnd(hbufBmkFile)
		    		if(True==BookmarksAdd (bmItem.Name, bmItem.File, bmItem.ln, bmItem.ich)){
		    			bmkCnt=bmkCnt+1
			    	}
	    		}
	    		else{
				DbgMsg("BookmarksAdd faild! ", bmItem)
	    		}
	    	}
	}

	CloseBuf(bufSch)
	
	SetCurrentWnd(hwnd)
	Msg("add @bmkCnt@ bookmarks")
}


